<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Section 4.6.3: Find the fastest mixing Markov chain on a graph</title>
<link rel="canonical" href="http://cvxr.com/cvx/examples/cvxbook/Ch04_cvx_opt_probs/html/fastest_mixing_MC.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Section 4.6.3: Find the fastest mixing Markov chain on a graph</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
Plots
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Boyd &amp; Vandenberghe "Convex Optimization"</span>
<span class="comment">% Jo&euml;lle Skaf - 09/26/05</span>
<span class="comment">%</span>
<span class="comment">% The 'fastest mixing Markov chain problem' is to find a transition</span>
<span class="comment">% probability matrix P on a graph E that minimizes the mixing rate r, where</span>
<span class="comment">% r = max{ lambda_2, -lambda_n } with lambda_1&gt;=...&gt;=lambda_n being the</span>
<span class="comment">% eigenvalues of P.</span>

<span class="comment">% Generate input data</span>
n = 5;
E = [0 1 0 1 1; <span class="keyword">...</span>
     1 0 1 0 1; <span class="keyword">...</span>
     0 1 0 1 1; <span class="keyword">...</span>
     1 0 1 0 1; <span class="keyword">...</span>
     1 1 1 1 0];

<span class="comment">% Create and solve model</span>
cvx_begin
    variable <span class="string">P(n,n)</span> <span class="string">symmetric</span>
    minimize(norm(P - (1/n)*ones(n)))
    P*ones(n,1) == ones(n,1);
    P &gt;= 0;
    P(E==0) == 0;
cvx_end
e = flipud(eig(P));
r = max(e(2), -e(n));

<span class="comment">% Display results</span>
disp(<span class="string">'------------------------------------------------------------------------'</span>);
disp(<span class="string">'The transition probability matrix of the optimal Markov chain is: '</span>);
disp(P);
disp(<span class="string">'The optimal mixing rate is: '</span>);
disp(r);
</pre>
<a id="output"></a>
<pre class="codeoutput">
 
Calling SDPT3: 68 variables, 9 equality constraints
   For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

 num. of constraints =  9
 dim. of sdp    var  = 10,   num. of sdp  blk  =  1
 dim. of linear var  =  8
 dim. of free   var  =  5 *** convert ublk to lblk
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
   HKM      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|5.1e+01|2.1e+01|3.8e+03| 1.600000e+01  0.000000e+00| 0:0:00| chol  1  1 
 1|0.929|0.955|3.7e+00|1.0e+00|1.7e+02| 2.267598e+01 -9.470840e+00| 0:0:00| chol  1  1 
 2|1.000|0.951|1.8e-06|6.0e-02|1.9e+01| 9.706394e+00 -7.808212e+00| 0:0:00| chol  1  1 
 3|0.994|0.277|2.7e-06|4.4e-02|7.5e+00| 6.612292e-01 -6.215577e+00| 0:0:00| chol  1  1 
 4|1.000|0.873|5.0e-06|5.6e-03|9.5e-01| 3.400135e-02 -8.928562e-01| 0:0:00| chol  1  1 
 5|0.677|0.489|1.6e-06|2.9e-03|4.5e-01|-5.555305e-01 -9.948414e-01| 0:0:00| chol  1  1 
 6|0.986|0.940|8.9e-08|1.7e-04|2.0e-02|-7.414098e-01 -7.608264e-01| 0:0:00| chol  1  1 
 7|0.988|0.988|4.8e-09|2.2e-06|2.4e-04|-7.498951e-01 -7.501312e-01| 0:0:00| chol  1  1 
 8|0.989|0.989|3.1e-10|5.3e-06|8.3e-06|-7.499988e-01 -7.500014e-01| 0:0:00| chol  1  2 
 9|1.000|0.989|1.1e-12|1.8e-07|2.6e-07|-7.499999e-01 -7.500000e-01| 0:0:00| chol  2  2 
10|1.000|0.989|9.6e-13|5.8e-09|7.9e-09|-7.500000e-01 -7.500000e-01| 0:0:00|
  stop: max(relative gap, infeasibilities) &lt; 1.49e-08
-------------------------------------------------------------------
 number of iterations   = 10
 primal objective value = -7.49999998e-01
 dual   objective value = -7.50000000e-01
 gap := trace(XZ)       = 7.90e-09
 relative gap           = 3.16e-09
 actual relative gap    = 8.22e-10
 rel. primal infeas     = 9.59e-13
 rel. dual   infeas     = 5.78e-09
 norm(X), norm(y), norm(Z) = 1.1e+00, 8.3e-01, 2.8e+00
 norm(A), norm(b), norm(C) = 1.0e+01, 2.0e+00, 2.5e+00
 Total CPU time (secs)  = 0.21  
 CPU time per iteration = 0.02  
 termination code       =  0
 DIMACS: 9.6e-13  0.0e+00  1.1e-08  0.0e+00  8.2e-10  3.2e-09
-------------------------------------------------------------------
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.75
 
------------------------------------------------------------------------
The transition probability matrix of the optimal Markov chain is: 
         0    0.3750         0    0.3750    0.2500
    0.3750         0    0.3750         0    0.2500
         0    0.3750         0    0.3750    0.2500
    0.3750         0    0.3750         0    0.2500
    0.2500    0.2500    0.2500    0.2500         0

The optimal mixing rate is: 
    0.7500

</pre>
</div>
</body>
</html>